<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="发起时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="发起场站" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入发起场站"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号码" prop="carNumber">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处置状态" prop="handlingStatus">
        <el-select v-model="queryParams.handlingStatus" placeholder="请选择处置状态" clearable size="small">
          <el-option label="全部" value="" />
          <el-option label="待处置" value="0" />
          <el-option label="已处置" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item  label="所属区域" prop="areaCode">
        <el-select v-model="queryParams.areaCode"  placeholder="请选择">
          <el-option
            v-for="item in jurisdictionUnitData"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="informationList" :max-height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column label="场站名称" align="left" prop="stationName" width="220"/>
<!--      <el-table-column label="运单编号" align="center" prop="waybillNum" width="180"/>-->
      <el-table-column label="车牌号码" align="center" prop="carNumber" />
      <el-table-column label="驾驶员" align="center" prop="driverName" />
      <el-table-column label="联系电话" align="center" prop="driverPhone" />
      <el-table-column label="运单发起时间" align="center" prop="initiationTime" width="160"/>
      <el-table-column label="发起人" align="center" prop="sendName" width="100"/>
      <el-table-column label="报警原因" align="center" prop="alarmReason" width="180"/>
      <el-table-column label="处置状态" align="center" prop="handlingStatus"  :formatter="typeFormat"/>
      <el-table-column label="所属区域" align="center" prop="areaCode" :formatter="converJurisdictionUnit"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            v-hasRole="['admin','wxygc','emergency_manage','traffic_manage','xqygc','jyygc','yxygc','wx_admin']"
            v-if="scope.row.handlingStatus=='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >处置</el-button>-->
          <el-button
            v-if="scope.row.handlingStatus=='1'"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleResultView(scope.row)"
          >处置结果</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >运单详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运单报警信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="运单编号" prop="waybillNum">
          <el-input v-model="form.waybillNum" placeholder="请输入运单编号" />
        </el-form-item>
        <el-form-item label="报警类型">
          <el-select v-model="form.alarmType" placeholder="请选择报警类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="报警原因" prop="alarmReason">
          <el-input v-model="form.alarmReason" placeholder="请输入报警原因" />
        </el-form-item>
        <el-form-item label="报警时间" prop="alarmTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.alarmTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择报警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处置状态">
          <el-radio-group v-model="form.handlingStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处置人" prop="handlingPerson">
          <el-input v-model="form.handlingPerson" placeholder="请输入处置人" />
        </el-form-item>
        <el-form-item label="处置时间" prop="handlingTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.handlingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择处置时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处置结果" prop="handlingResult">
          <el-input v-model="form.handlingResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标志位" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志位" />
        </el-form-item>
        <el-form-item label="创建人" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateId">
          <el-input v-model="form.updateId" placeholder="请输入更新人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--警报处置-->
    <el-dialog title="预警处置意见" :visible.sync="openView" width="500px">
      <el-input v-model="form.handlingResult" :rules="rules" type="textarea"  :rows="8" placeholder="请输入处置意见" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="form.handlingStatus == '0'" @click="submitForm2">确 定</el-button>
        <el-button @click="openView = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInformation, getInformation, delInformation, addInformation, updateInformation, exportInformation } from "@/api/weight/information";
import { getAreaListByUserId } from "@/api/weight/station";

export default {
  name: "Information",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 运单报警信息表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      tableHeight: window.innerHeight - 275,
      //日期范围
      dateRange: [],
      // 管辖单位
      jurisdictionUnitData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        waybillNum: undefined,
        handlingStatus: undefined,
        stationName: undefined,
        carNumber: undefined,
        areaCode: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        handlingResult: [
          { required: true, message: "处置内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    let date = new Date();
    let today= this.timeFormat(date) //当前日期
    let firstDayOfMonth = this.getFirstDayOfMonth(date) //本月第一天
    // 默认日期赋值
    this.dateRange = [firstDayOfMonth, today];

    const handlingStatus = this.$route.query && this.$route.query.handlingStatus;
    this.queryParams.handlingStatus=handlingStatus;
    this.getList();

    getAreaListByUserId().then(response =>{
      this.jurisdictionUnitData=response.data;
    });
  },
  methods: {
    /** 查询运单报警信息列表 */
    getList() {
      this.loading = true;
      listInformation(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        waybillNum: undefined,
        alarmType: undefined,
        alarmReason: undefined,
        alarmTime: undefined,
        handlingStatus: '',
        handlingPerson: undefined,
        handlingTime: undefined,
        handlingResult: undefined,
        delFlag: undefined,
        createTime: undefined,
        createId: undefined,
        updateTime: undefined,
        updateId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      let date = new Date();
      let today= this.timeFormat(date) //当前日期
      let firstDayOfMonth = this.getFirstDayOfMonth(date) //本月第一天
      // 默认日期赋值
      this.dateRange = [firstDayOfMonth, today];
      this.handleQuery();
    },
    // 处理状态
    typeFormat(row, column) {
      if (row.handlingStatus == '0')
        return  '待处置'
      if (row.handlingStatus == '1')
        return  '已处置'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运单报警信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.handlingResult = '';
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data;
        this.openView = true;
        // this.title = "修改运单报警信息";
      });
    },
    handleResultView(row) {
      this.reset();
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data;
        this.openView = true;
      });
    },
    // 处置警报
    submitForm2:function(){
      if (this.form.handlingResult.length <=0){
        this.msgError('处置内容不能为空');
      }else {
        if (this.form.id != undefined) {
          this.form.handlingStatus='1'
          updateInformation(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("处置成功");
              this.openView = false;
              this.getList();
            } else {
              this.msgError(response.msg);
            }
          });
        }
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateInformation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addInformation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const waybillId = row.waybillId || this.ids
      this.$router.push({
        path:"/weigh/waybilldetail",
        query:{
          id:waybillId
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除运单报警信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInformation(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有运单报警信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInformation(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    timeFormat: function (date) {
      if (!date || typeof(date) === "string") {
        this.error("参数异常，请检查...");
      }
      var y = date.getFullYear(); //年
      var m = date.getMonth() + 1; //月
      var d = date.getDate(); //日

      return y + "-" + (m < 10 ? '0' + m : m) + "-" + (d < 10 ? '0' + d : d);
    },
    //获取当月第一天
    getFirstDayOfMonth: function (date) {
      date.setDate(1);
      return this.timeFormat(date);
    },
    // 所属区域转换
    converJurisdictionUnit(row,column){
      let areaName = '';
      this.jurisdictionUnitData.forEach(function (_val,index) {
        if(row.areaCode==_val.areaCode){
          areaName = _val.areaName;
          return true;
        }
      });
      return areaName;
    },
  }
};
</script>

